<?php
include_once('./config/config.php');
$admin = AuthCheck();
$action = $_GET['action'] ?? '';
// 退出账号
if ($action === 'logout') {
    setcookie('auth', '', time() - 1);
    notice('退出成功', 'login.php');
}

// 当前页
$page = $_GET['page'] ?? 1;
// 查询总条数
$CountSql = "SELECT COUNT(*) AS `c` FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id`";
$count = find($CountSql);
$limit = 10; // 限制显示/查找10条数据
// LIMIT 5（显示5条） LIMIT 1(索引值) 5 LIMIT $page $limit
$start = ($page - 1) * $limit; // 从第几条数据开始查找
$PersonSql = "SELECT `p`.*,`d`.`name` AS `dname`,`j`.`name` AS `jname` FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` ORDER BY `p`.`id`  LIMIT $start,$limit";
$PersonData = all($PersonSql);
$ShowPage = ShowPage($page, $count['c'], $limit); // 分页器

// 搜索功能
$search = $_GET['search'] ?? '';

if ($search) {
    // 构建 SQL 查询
    $searchSql = "SELECT `p`.*,`d`.`name` AS `dname`,`j`.`name` AS `jname` FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` WHERE `p`.`name` LIKE '%$search%' ORDER BY `p`.`id`  LIMIT $start,$limit";
    // 查询总条数
    $CountSql = "SELECT COUNT(*) AS `c` FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` WHERE `p`.`name` LIKE '%$search%'";
    $count = find($CountSql);
    $PersonData = all($searchSql);
    $ShowPage = ShowPage($page, $count['c'], $limit); // 分页器
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>admin</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- 公共样式 -->
    <?php include_once('./common/meta.php'); ?>

    <!-- jquery -->
    <script src="./assets/lib/jquery-1.7.2.min.js" type="text/javascript"></script>

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="../assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">
</head>

<!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
<!--[if IE 7 ]> <body class="ie ie7 "> <![endif]-->
<!--[if IE 8 ]> <body class="ie ie8 "> <![endif]-->
<!--[if IE 9 ]> <body class="ie ie9 "> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->

<body>
    <!--<![endif]-->

    <!-- 公共头部 -->
    <?php include_once('./common/header.php') ?>

    <!-- 公共侧边菜单栏 -->
    <?php include_once('./common/menu.php') ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">员工列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">PersonList</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='PersonAdd.php'"><i class="icon-plus"></i>添加员工</button>
                    <input name="search" id="search" style="margin-left: 10px;">
                    <button class="btn btn-primary" id="searchBtn" style="margin-left: 5px;">搜索</button>
                </div>

                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>员工姓名</th>
                                <th>手机号</th>
                                <th>性别</th>
                                <th>邮箱</th>
                                <th>部门</th>
                                <th>职位</th>
                                <th>入职时间</th>
                                <th style="width: 30px;">操作</th>
                            </tr>
                        </thead>
                        <tbody>

                            <?php foreach ($PersonData as $item) { ?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td>
                                        <a href="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/avatar.png'; ?>" target="_blank">
                                            <img src="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/avatar.png'; ?>" style="width: 25px;height:25px;border-radius:50%;object-fit: cover;" alt="">
                                        </a>
                                    </td>
                                    <td><?php echo $item['name']; ?></td>
                                    <td><?php echo $item['mobile']; ?></td>
                                    <td><?php echo $item['sex'] == 1 ? '男' : ($item['sex'] == 2 ? '女' : '保密'); ?></td>
                                    <td><?php echo $item['email']; ?></td>
                                    <td><?php echo $item['dname']; ?></td>
                                    <td><?php echo $item['jname']; ?></td>
                                    <td><?php echo date('Y-m-d', $item['createtime']); ?></td>
                                    <td>
                                        <a href="PersonEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" role="button" data-toggle="modal"><i class="icon-remove" data-id="<?php echo $item['id']; ?>"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>

                    </table>
                </div>
                <div class="pagination">
                    <?php echo $ShowPage; ?>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog"
                    aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除该员工</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger" data-dismiss="modal">确认</button>
                    </div>
                </div>

                <!-- 公共底部 -->
                <?php include_once('./common/footer.php') ?>
            </div>
        </div>
    </div>

</body>

</html>
<!-- 公共js -->
<?php include_once('./common/script.php') ?>
<script>
    // 删除功能
    // 获取要删除的员工id
    let id = 0
    $('.icon-remove').on('click', function() {
        id = $(this).data('id')
    })

    // 发请求
    $('.btn-danger').click(() => {
        // console.log(id)
        $.ajax({
            url: 'api.php',
            type: 'post',
            data: {
                id,
                action: 'person_delete'
            },
            dataType: 'json',
            success: res => {
                if (res.code === 1) {
                    alert(res.msg)
                    location.href = 'PersonList.php'
                } else alert(res.msg)
            }
        })
    })

    // 搜索功能
    $('#searchBtn').click(() => {
        // 获取input框的值
        const ipt = $('#search').val() ? $.trim($('#search').val()) : ''
        location.href = `PersonList.php?search=${ipt}`
    })
</script>